﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdexApiClient.api.v2
{
    public partial class EdexApiContext
    {

        /// <summary>
        /// Получить перечень складов
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>       
        public async Task<CommonApiResponse<WH_LOCATIONS>> GetLocationDetails(Guid id)
        {
            var uri = String.Format("v2/locations/{0}/?api_key={1}", id.ToString(), this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            var data = await this.GetData<WH_LOCATIONS>(uri);
            return data;
        }

        public async Task<CommonApiResponse> AddLocation(WH_LOCATIONS data)
        {
            var uri = String.Format("WH_LOCATIONS/?api_key={0}", this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Post<WH_LOCATIONS>(uri, data);
        }

        public async Task<CommonApiResponse> UpdateLocation(WH_LOCATIONS data)
        {
            var uri = String.Format("WH_LOCATIONS/?api_key={0}", this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Put<WH_LOCATIONS>(uri, data);
        }

        public async Task<CommonApiResponse> DeleteLocation(string id)
        {
            var uri = String.Format("WH_LOCATIONS/{0}/?api_key={1}", id, this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Delete(uri);
        }

        public async Task<CommonApiResponse<WH_LOCATIONS>> GetLocations(
            string filter = null,           
            bool search_IS_ALL = false,
            string[] expand = null,
            string[] orderby = null)
        {
            var uri = String.Format("v2/locations/?api_key={0}", this.api_key);
                       
            var _query = String.Empty;            
            var _queryExpand = "COMPANY";
            var _queryOrderBy = "LOCATION_NAME";
            var _queryTop = "100";
            
            if (!String.IsNullOrEmpty(filter))
            {
                filter = filter.Remove(filter.Length - 2);
                _query += "&$filter=" + filter;
            }      

            _query += "&$expand=" + _queryExpand;
            _query += "&$orderby=" + _queryOrderBy;

            if (search_IS_ALL == false)
                _query += "&$top=" + _queryTop;

            uri += _query;

            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.GetData<WH_LOCATIONS>(uri);
        }
    }
}
